﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_AdminNava_Manage : System.Web.UI.Page
{
    public string NavaAll;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            populateDropdownlist();
            populateRootNava();
        }
    }
    public void populateDropdownlist()
    {

        drpAdminRole.DataSource = AdminRole.GetAll();
        drpAdminRole.DataValueField = "ID";
        drpAdminRole.DataTextField = "RoleName";
        drpAdminRole.DataBind();
        drpAdminRole.Items.Insert(0, new ListItem("请选择", "0"));

    }
    public void populateRootNava()
    {
        rptRootNava.DataSource = Navigation.GetAllRootNava();
        rptRootNava.DataBind();
    }
    protected void rptRootNava_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HiddenField hdlID = (HiddenField)e.Item.FindControl("hdlID");
            Repeater rptParNava = (Repeater)e.Item.FindControl("rptParNava");
            rptParNava.DataSource = Navigation.GetAllByParentID(DongDong.Common.Misc.ConvertToInt(hdlID.Value),"");
            rptParNava.DataBind();
        }
    }

    protected void rptParNava_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HiddenField hdlID = (HiddenField)e.Item.FindControl("hdlID");
            Repeater rptNava = (Repeater)e.Item.FindControl("rptNava");
            rptNava.DataSource = Navigation.GetAllByParentID(DongDong.Common.Misc.ConvertToInt(hdlID.Value),"");
            rptNava.DataBind();
        }
    }
    protected void drpAdminRole_SelectedIndexChanged(object sender, EventArgs e)
    {
       
        int ID = DongDong.Common.Misc.ConvertToInt(drpAdminRole.SelectedValue);
        if (ID != 0)
        {
            Array NavaAllList = AdminNava.GetAllByAdminRoleID(ID).Select(row => row.NavaID).ToArray();
            if (NavaAllList.Length == 0)
                return;
            foreach (int i in NavaAllList)
            {
                NavaAll += i + "_";
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        NavaAll = "";
        int ID = DongDong.Common.Misc.ConvertToInt(drpAdminRole.SelectedValue);
        if (ID != 0)
        {
            AdminNava.DeleteAllByRoleID(ID);
            string Nava = hdlNavaAll.Value;
            if (string.IsNullOrEmpty(Nava))
                return;
            string[] lists = Nava.Split('_');
            for (int i = 0; i < lists.Length - 1; i++)
            {
                AdminNava objAdminNava = new AdminNava();
                objAdminNava.NavaID = DongDong.Common.Misc.ConvertToInt(lists[i]);
                objAdminNava.RoleID = ID;
                objAdminNava.Add();
                drpAdminRole_SelectedIndexChanged(null, null);
            }
        }
        
       
    }
 
}